package com.ygqh.baby.service.impl;

import com.foxinmy.weixin4j.util.StringUtil;
import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.PayType;
import com.ygqh.baby.mapper.YgOrderPayLogMapper;
import com.ygqh.baby.po.YgOrderPayLog;
import com.ygqh.baby.po.YgOrderPayLogExample;
import com.ygqh.baby.po.YgOrderPayment;
import com.ygqh.baby.service.YgOrderPayLogService;
import com.ygqh.baby.service.YgOrderPaymentService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 〈〉
 *
 * @author guohao
 * @date 2019/3/18 11:24
 * @since 1.0.0
 */
@Service
public class YgOrderPayLogServiceImpl implements YgOrderPayLogService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    ExecutorService executorService = Executors.newFixedThreadPool(5);

    @Autowired
    private YgOrderPaymentService ygOrderPaymentService;

    @Autowired
    private YgOrderPayLogMapper ygOrderPayLogMapper;

    @Override
    public int addPayLog(String paymentCode, String orderId, PayType payType, String logType, String content) {
        executorService.execute(() -> {
            YgOrderPayLog payLog = null;
            try {
                payLog = new YgOrderPayLog();
                payLog.setPaymentCode(paymentCode);
                if (StringUtil.isBlank(orderId)) {
                    YgOrderPayment orderPayment = ygOrderPaymentService.findByPayCode(paymentCode);
                    payLog.setOrderId(orderPayment.getOrderId());
                } else {
                    payLog.setOrderId(orderId);
                }
                payLog.setPayType(payType.name());
                payLog.setLogType(logType);
                payLog.setContent(content);
                payLog.setCreateTime(new Date());
                payLog.setStatus(DataStatus.Valid.name());

                if ("Notify".equals(logType)) {
                    updateOrderId(paymentCode, payLog.getOrderId());
                }
                ygOrderPayLogMapper.insertSelective(payLog);
            } catch (Exception e) {

                logger.error("addPayLog fail: {} , log: {}", e.getMessage(), payLog);
            }

        });
        return 1;
    }

    private void updateOrderId(String paymentCode, String orderId) {
        YgOrderPayLogExample example = new YgOrderPayLogExample();
        example.createCriteria().andPaymentCodeEqualTo(paymentCode);
        YgOrderPayLog log = new YgOrderPayLog();
        log.setOrderId(orderId);
        ygOrderPayLogMapper.updateByExampleSelective(log, example);
    }

    @Override
    public int addPayLog(List<YgOrderPayLog> logs) {
        if (CollectionUtils.isEmpty(logs)) {
            return 0;
        }
        executorService.execute(() -> {
            try {
                ygOrderPayLogMapper.addBatch(logs);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return 1;
    }
}
